using System;
using System.Collections.Generic;
using Emlyn.Website.Users;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Emlyn.Website.Base;
using System.Drawing;
using System.Web;
using Emlyn.Common.Tools.Web;

namespace Emlyn.Website.UsersPresentation
{
    public class PresentUser : PresentBase
    {
        TextBox _txtUserName;
        DropDownList _ddlAuthLevel;
        TextBox _txtEmail;

        public void OnUpdate(Object o, System.EventArgs ea)
        {
            User luser = aUser;
            string lerrorMessage = null;

            UserQuery luq = new UserQuery();

            User llookupUser = luq.GetUserByUserName(_txtUserName.Text);
            if (llookupUser != null && llookupUser.UserID != luser.UserID)
            {
                lerrorMessage = "UserName already exists";
            }

            if (lerrorMessage == null)
            {
                luser.UserName = _txtUserName.Text;

                luser.AuthLevel = (GlobalAuthLevel)Enum.Parse(typeof(GlobalAuthLevel), _ddlAuthLevel.SelectedValue);
                if (luser.Email != _txtEmail.Text)
                {
                    luser.Email = _txtEmail.Text;
                    luser.CreateValidationCode();
                }

                UserMod lum = new UserMod();

                lum.Save(luser);

                Page.Response.Redirect(UriBuilder2.RemoveQueryItem(Page.Request.Url, "ErrorMessage").ToString());
            }
            else
            {
                Page.Response.Redirect(UriBuilder2.AddQueryItem(Page.Request.Url, "ErrorMessage", lerrorMessage).ToString());
            }
        }

        public void OnDelete(Object o, System.EventArgs ea)
        {
            UserMod lum = new UserMod();
            lum.Delete(aUser);

            Page.Response.Redirect(UriBuilder2.RemoveQueryItem(Page.Request.Url, "ErrorMessage").ToString());
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string UrlPrefix
        {
            get { return Convert.ToString(ViewState["UrlPrefix"]); }
            set { ViewState["UrlPrefix"] = value; }
        }

        public string ErrorMessage
        {
            get { return Page.Request.QueryString["ErrorMessage"]; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public User aUser
        {
            get
            {
                User retval = null;

                string luserGuid = Convert.ToString(ViewState["UserGuid"]);
                if (!String.IsNullOrEmpty(luserGuid))
                {
                    UserQuery luq = new UserQuery();
                    User luser = luq.Select(new Guid(luserGuid));
                    retval = luser;
                }

                return retval;
            }
            set
            {
                ViewState["UserGuid"] = value.UserID.ToString();
            }
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            User luser = aUser;

            if (luser != null && !String.IsNullOrEmpty(UrlPrefix))
            {
                Panel lbasePanel = new Panel();
                lbasePanel.CssClass = CssClass + "UserDetail";
                Controls.Add(lbasePanel);

                Panel lvalPanel;
                Label lblValue;

                if (!String.IsNullOrEmpty(ErrorMessage))
                {
                    lvalPanel = ConstructAttributeControls(luser, lbasePanel, "Error:");
                    lblValue = new Label();
                    lblValue.Text = ErrorMessage;
                    lvalPanel.Controls.Add(lblValue);
                }

                lvalPanel = ConstructAttributeControls(luser, lbasePanel, "User Name:");

                if (Editable)
                {
                    _txtUserName = new TextBox();

                    _txtUserName.Text = luser.UserName;
                    lvalPanel.Controls.Add(_txtUserName);
                }
                else
                {
                    lblValue = new Label();
                    lblValue.Text = luser.UserName;
                    lvalPanel.Controls.Add(lblValue);
                }

                lvalPanel = ConstructAttributeControls(luser, lbasePanel, "Auth Level:");

                if (Editable)
                {
                    _ddlAuthLevel = new DropDownList();

                    ListItem lselectedItem = null;

                    foreach(GlobalAuthLevel lauthLevel in Enum.GetValues(typeof(GlobalAuthLevel)))
                    {
                        ListItem lli = new ListItem(lauthLevel.ToString());
                        _ddlAuthLevel.Items.Add(lli);

                        if (lauthLevel == luser.AuthLevel)
                            lselectedItem = lli;
                    }

                    if (lselectedItem != null)
                        lselectedItem.Selected = true;

                    lvalPanel.Controls.Add(_ddlAuthLevel);
                }
                else
                {
                    lblValue = new Label();
                    lblValue.Text = luser.AuthLevel.ToString();
                    lvalPanel.Controls.Add(lblValue);
                }

                lvalPanel = ConstructAttributeControls(luser, lbasePanel, "Validated:");

                lblValue = new Label();
                lblValue.Text = luser.Validated.ToString();
                lvalPanel.Controls.Add(lblValue);

                lvalPanel = ConstructAttributeControls(luser, lbasePanel, "Email:");

                if (Editable)
                {
                    _txtEmail = new TextBox();
                    _txtEmail.Text = luser.Email;
                    lvalPanel.Controls.Add(_txtEmail);
                }
                else
                {
                    lblValue = new Label();
                    lblValue.Text = luser.Email;
                    lvalPanel.Controls.Add(lblValue);
                }

                if (Editable)
                {
                    Panel lpnl = new Panel();
                    lpnl.CssClass = "UserManagementDetailSubmit";

                    Button lbutton = new Button();
                    lbutton.Text = "Update";
                    lbutton.Click += new EventHandler(OnUpdate);
                    lbutton.CssClass = lpnl.CssClass + "UpdateButton";

                    lpnl.Controls.Add(lbutton);

                    lbutton = new Button();
                    lbutton.Text = "Delete";
                    lbutton.Click += new EventHandler(OnDelete);
                    lbutton.OnClientClick = "return confirm('Really delete this user permanently?');";
                    lbutton.CssClass = lpnl.CssClass + "DeleteButton";

                    lpnl.Controls.Add(lbutton);

                    lbasePanel.Controls.Add(lpnl);
                }
            }
        }

        private static Panel ConstructAttributeControls(User aaUser, Panel aBasePanel, string aKeyDisplayName)
        {
            Panel lkeyPanel;
            Label lblKey;
            Panel lvalPanel;

            lkeyPanel = new Panel();
            lkeyPanel.CssClass = "UserManagementDetailKey";
            aBasePanel.Controls.Add(lkeyPanel);

            lblKey = new Label();
            lblKey.Text = aKeyDisplayName; // "Validated:";
            lkeyPanel.Controls.Add(lblKey);

            lvalPanel = new Panel();
            lvalPanel.CssClass = "UserManagementDetailValue";
            aBasePanel.Controls.Add(lvalPanel);

            return lvalPanel;
        }
    }
}
